// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//
#pragma once

#include <pollux/common/file/file.h>
#include <pollux/connectors/hive/storage_adapters/abfs/AzureDataLakeFileClient.h>

namespace kumo::pollux::config {
class ConfigBase;
}

namespace kumo::pollux::filesystems {

/// We are using the DFS (Data Lake Storage) endpoint for Azure Blob File write
/// operations because the DFS endpoint is designed to be compatible with file
/// operation semantics, such as `Append` to a file and file `Flush` operations.
/// The legacy Blob endpoint can only be used for blob level append and flush
/// operations. When using the Blob endpoint, we would need to manually manage
/// the creation, appending, and committing of file-related blocks.

/// Implementation of abfs write file. Nothing written to the file should be
/// read back until it is closed.
class AbfsWriteFile : public WriteFile {
 public:
  constexpr static uint64_t kNaturalWriteSize = 8 << 20; // 8M

  /// @param path The file path to write.
  /// @param connectStr The connection string used to auth the storage account.
  AbfsWriteFile(std::string_view path, const config::ConfigBase& config);

  /// @param path The file path to write.
  /// @param client The AdlsFileClient.
  AbfsWriteFile(
      std::string_view path,
      std::unique_ptr<AzureDataLakeFileClient>& client);

  ~AbfsWriteFile();

  /// Get the file size.
  uint64_t size() const override;

  /// Flush the data.
  void flush() override;

  /// Write the data by append mode.
  void append(std::string_view data) override;

  /// Close the file.
  void close() override;

 protected:
  class Impl;
  std::unique_ptr<Impl> impl_;
};
} // namespace kumo::pollux::filesystems
